/******************************************************************************
** 
** Expand.
** 
** File name:	expand.c
** Compilation:	cc expand.c -o expand
** Execution:	./expand
** 
** Description:
** Expands strings like "a-z0-9".
** 
******************************************************************************/
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include "myfunctions.h"

#define STR_MAX_LEN 255

void Expand(char s1[], char s2[]);

int main()
{
    char s1[STR_MAX_LEN];
    char s2[STR_MAX_LEN];
    unsigned short int s1len;

    while(s1len = GetString(s1, STR_MAX_LEN)) {
        Expand(s1, s2);
        printf("Modified: %s\n", s2);
    }


    return 0;
}

void Expand(char s1[], char s2[]) {
    unsigned short int i, j;
    char c;

    i = 0;
    j = 0;
    while ((s1[i] != ENDLINE_SYMBOL) && (i <= STR_MAX_LEN)) {
        if ((s1[i] == '-') && (i != 0) && (s1[i+1] != ENDLINE_SYMBOL)) { // it's a control symbol
            if (s1[i-1] < s1[i+1]) {
                for (c = s1[i-1]+1; c <= s1[i+1]-1 ; c++) {
                    if (isalnum(c)) {
                        s2[j] = c;
                        j++;
                    }
                }
            } else if (s1[i-1] > s1[i+1]) {
                for (c = s1[i-1]-1; c >= s1[i+1]+1; c--) {
                    if (isalnum(c)) {
                        s2[j] = c;
                        j++;
                    }
                }
            } else {
                s2[j] = s1[i];
                j++;
            }
        } else {
            s2[j] = s1[i];
            j++;
        }
        i++;
    }
    s2[j] = ENDLINE_SYMBOL;
}
